//
//  TravePictureViewController.m
//  Travelmemory
//
//  Created by lanou3g on 16/3/31.
//  Copyright © 2016年 ABCD. All rights reserved.
//

#import "TravePictureViewController.h"
#import "TraveNoteModel.h"
#import "UMSocialDataService.h"
#import "UIImageView+WebCache.h"
#define kScreenW  self.view.bounds.size.width
#define kScreenH self.view.bounds.size.height

@interface TravePictureViewController ()<UIScrollViewDelegate,UMSocialDataDelegate>


@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIImageView *imageV;
/** 进行分享或者要保存的图片 */
@property (nonatomic, copy) NSString *pictureUrl;

@end

@implementation TravePictureViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = YES;
}

- (UITextView *)textView {
    if (!_textView) {
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.scrollView.frame), kScreenW, kScreenH * 0.2)];
        self.textView.backgroundColor = [UIColor blackColor];
        self.textView.editable = NO;
        self.textView.textColor = [UIColor whiteColor];
        self.textView.font = [UIFont systemFontOfSize:16];
        self.textView.text = [self.picArray[self.page] text];
        
    }
    return _textView;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, kScreenW, kScreenH * 0.6)];
        self.scrollView.contentSize = CGSizeMake(self.picArray.count * kScreenW, 0);
        self.scrollView.pagingEnabled = YES;
        self.scrollView.bounces = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:self.scrollView];
        self.scrollView.contentOffset = CGPointMake(self.page * kScreenW, self.scrollView.frame.size.height);
        self.scrollView.delegate = self;
        
    }
    return _scrollView;
}





- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(dismissAction)];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    self.label.text = [NSString stringWithFormat:@"%lu / %lu张",self.page + 1, (unsigned long)self.picArray.count];
    self.label.textColor = [UIColor redColor];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = self.label;
    
    UIBarButtonItem *right1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStyleDone target:self action:@selector(shareAction)];
    UIBarButtonItem *right2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"load"] style:UIBarButtonItemStyleDone target:self action:@selector(saveImageAction)];
    self.navigationItem.rightBarButtonItems = @[right1, right2];
    
    self.pictureUrl = [self.picArray[self.page] photo_s];
    
    for (int i = 0; i < self.picArray.count; i++) {
        TraveNoteModel *model = self.picArray[i];
        
        self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(i * kScreenW, 0, kScreenW, self.scrollView.frame.size.height)];
        
        //[self.imageV sd_setImageWithURL:[NSURL URLWithString:model.photo]];
        [self.imageV sd_setImageWithURL:[NSURL URLWithString:model.photo_s] placeholderImage:[UIImage imageNamed:@"11"] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        [self.scrollView addSubview:self.imageV];
    }
    [self.view addSubview:self.textView];
    [self.view addSubview:self.button];
    
}

- (void)dismissAction {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark --- - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSUInteger i = scrollView.contentOffset.x / CGRectGetWidth(scrollView.bounds);
    self.label.text = [NSString stringWithFormat:@"%lu / %lu张",i + 1, (unsigned long)self.picArray.count];
    self.textView.text = [self.picArray[i] text];
    self.pictureUrl = [self.picArray[i] photo_s];
}


#pragma mark - 保存图片到系统相册
- (void)saveImageAction {
    
    dispatch_queue_t queue = dispatch_queue_create("loadImage",NULL);
    dispatch_async(queue, ^{
        
        NSData *resultData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.pictureUrl]];
        UIImage *img = [UIImage imageWithData:resultData];
        UIImageWriteToSavedPhotosAlbum(img, NULL, NULL, NULL);
        
    });
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
                                                    message:@"保存成功,请到相册查看"
                                                   delegate:self
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [self performSelector:@selector(alertDismiss:) withObject:alert afterDelay:1];
    [alert show];
    
}
// 自动取消alert
- (void)alertDismiss:(UIAlertView *)alert {
    if(alert) {
        [alert dismissWithClickedButtonIndex:[alert firstOtherButtonIndex] animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
